home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-07 | 11.7 KB | 172 lines | [TEXT/R*ch] |
- // A module of KPlib v1.2.1.
- // Written by Keith Pomakis during the summer of 1994.
- // Released to the public domain on October 10, 1994.
-
- #ifndef KP_SET_DEFINED
- #define KP_SET_DEFINED
-
- #include "KPbasic.h"
- #include "KPList.h"
-
- // Assumes Element has a default constructor, operator=(), operator==(),
- // and operator<(). Note that operator<() must place a total ordering on
- // the set of Elements.
-
- // All union, intersection and difference operations are of order O(n).
-
- template <class Element>
- class KPSet {
- public:
- KPSet();
- KPSet(const KPSet &set);
- KPSet(const KPList<Element> &list);
- KPSet(const Element &element);
- ~KPSet();
- operator KPList<Element>() const;
-
- // Union
- KPSet operator+(const KPSet &set) const;
- KPSet operator+(const Element &element) const;
- KPSet &operator+=(const KPSet &set);
- KPSet &operator+=(const Element &element);
- KPSet &add(const KPSet &set);
- KPSet &add(const Element &element);
-
- // Intersection
- KPSet operator*(const KPSet &set) const;
- KPSet operator*(const Element &element) const;
- KPSet &operator*=(const KPSet &set);
- KPSet &operator*=(const Element &element);
-
- // Difference
- KPSet operator-(const KPSet &set) const;
- KPSet operator-(const Element &element) const;
- KPSet &operator-=(const KPSet &set);
- KPSet &operator-=(const Element &element);
-
- // Miscellaneous
- KPSet &operator=(const KPSet &set);
- KPSet &operator=(const Element &element);
- KPSet &operator=(const KPList<Element> &list);
- const KPList<Element> &list() const;
- int size() const;
- bool is_empty() const;
- bool contains(const KPSet &set) const;
- bool contains(const Element &element) const;
- KPSet all_such_that(bool (*f)(const Element &)) const;
- KPSet &clear();
- protected:
- KPSortableList<Element> my_list;
- };
-
- /****************************************************************************/
-
- template <class Element>
- inline
- KPSet<Element>::KPSet(): my_list()
- { /* do nothing */ }
-
- /****************************************************************************/
-
- template <class Element>
- inline
- KPSet<Element>::KPSet(const KPSet<Element> &set): my_list(set.my_list)
- { /* do nothing */ }
-
- /****************************************************************************/
-
- template <class Element>
- inline
- KPSet<Element>::KPSet(const KPList<Element> &list)
- {
- *this = list;
- }
-
- /****************************************************************************/
-
- template <class Element>
- inline
- KPSet<Element>::KPSet(const Element &element): my_list(element)
- { /* do nothing */ }
-
- /****************************************************************************/
-
- template <class Element>
- inline
- KPSet<Element>::~KPSet()
- {
- my_list.clear();
- }
-
- /****************************************************************************/
-
- template <class Element>
- inline
- KPSet<Element>::operator KPList<Element>() const
- {
- return my_list;
- }
-
- /****************************************************************************/
-
- template <class Element>
- inline KPSet<Element>
- KPSet<Element>::operator+(const KPSet<Element> &set) const
- {
- KPSet<Element> new_set(*this);
- new_set += set;
- return new_set;
- }
-
- /****************************************************************************/
-
- template <class Element>
- inline KPSet<Element>
- KPSet<Element>::operator+(const Element &element) const
- {
- KPSet<Element> new_set(*this);
- new_set += element;
- return new_set;
- }
-
- /****************************************************************************/
-
- template <class Element>
- KPSet<Element> &
- KPSet<Element>::operator+=(const KPSet<Element> &set)
- {
- if (this == &set)
- return *this;
-
- KPIterator<Element> a(my_list);
- KPReadOnlyIterator<Element> b(set.my_list);
-
- while (b.ptr()) {
- while (a.ptr() && *a < *b)
- a++;
- if (!a.ptr()) {
- for (; b.ptr(); b++)
- my_list.add_to_tail(*b);
- }
- else {
- if (*a == *b)
- a++;
- else
- a.insert_before_current(*b);
- b++;
- }
- }
-
- return *this;
- }
-
- /****************************************************************************/
-
- template <class Element>
- KPSet<Element> &
- KPSet<Element>::operator+=(const Element &element)
- {
- KPIterator<Element> iter(my_list);
-
- // Start looking from the end of the list, in case the elements are
- // being added in a